/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormularioBuscar.java
 *
 * Created on 19/02/2011, 03:42:11 PM
 */

package vistas;

import clases.Estudiante;
import clases.Profesor;
import clases.Seccion;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author 
 */
public class FormularioBuscar extends javax.swing.JFrame {
     private Seccion seccion;


    public void setSeccion(Seccion seccion) {
        this.seccion = seccion;
    }

    /** Creates new form FormularioBuscar */
    public FormularioBuscar() {
        initComponents();
    }

     public void cancelar(){
        txtApe.setText("");
        txtCed.setText("");
        txtNom.setText("");
    }

        public boolean validarVacios(){
            if(txtCed.getText().equals(""))
                return false;
            else
                return true;
   }

   public boolean validarCedula(){
        try {
            if(Integer.parseInt(txtCed.getText())>0)
                return true;
            else
                return false;
	} catch (NumberFormatException e) {
            return false;
        }
   }

    public boolean validarEstudianteRegistrado(){
        if(seccion.buscarEstudiante(Integer.parseInt(txtCed.getText()))==null)
            return true;
        else
            return false;
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnBuscar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCed = new javax.swing.JTextField();
        txtNom = new javax.swing.JTextField();
        txtApe = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        btnRetirar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        btnBuscar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar);
        btnBuscar.setBounds(140, 200, 110, 30);

        jLabel3.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        jLabel3.setText("Apellido:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(80, 160, 100, 18);

        jLabel2.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        jLabel2.setText("Nombre:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(80, 110, 90, 18);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        jLabel1.setText("Cedula:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(80, 50, 110, 18);

        txtCed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCedActionPerformed(evt);
            }
        });
        getContentPane().add(txtCed);
        txtCed.setBounds(210, 50, 90, 28);
        getContentPane().add(txtNom);
        txtNom.setBounds(210, 100, 90, 28);
        getContentPane().add(txtApe);
        txtApe.setBounds(210, 150, 90, 28);

        btnCancelar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(140, 280, 110, 30);

        btnSalir.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        getContentPane().add(btnSalir);
        btnSalir.setBounds(140, 320, 110, 30);

        btnRetirar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnRetirar.setText("Retirar");
        btnRetirar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRetirarActionPerformed(evt);
            }
        });
        getContentPane().add(btnRetirar);
        btnRetirar.setBounds(140, 240, 110, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-396)/2, (screenSize.height-401)/2, 396, 401);
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
            if(validarVacios())
            if(validarCedula())
                 if(!validarEstudianteRegistrado()){
                    Estudiante e = seccion.buscarEstudiante(Integer.parseInt(txtCed.getText()));
                    txtCed.setText(""+e.getCedula());
                    txtNom.setText(e.getNombre());
                    txtApe.setText(e.getApellido());

                }
                else
                    JOptionPane.showMessageDialog(new JFrame(),"La cedula no esta registrada","Buscar",JOptionPane.WARNING_MESSAGE);
            else
                JOptionPane.showMessageDialog(new JFrame(),"Cedula invalida","Buscar",JOptionPane.WARNING_MESSAGE);
        else
            JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Buscar",JOptionPane.WARNING_MESSAGE);

}//GEN-LAST:event_btnBuscarActionPerformed

    private void txtCedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCedActionPerformed
        // TODO add your handling code here:
      
}//GEN-LAST:event_txtCedActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        cancelar();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        dispose();
}//GEN-LAST:event_btnSalirActionPerformed

    private void btnRetirarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRetirarActionPerformed
        // TODO add your handling code here:
            if(validarVacios())
            if(validarCedula())
                 if(!validarEstudianteRegistrado()){
                    seccion.retirarEstudiante(Integer.parseInt(txtCed.getText()));
                    JOptionPane.showMessageDialog(new JFrame(),"Estudiante retirado","Retirar",JOptionPane.INFORMATION_MESSAGE);
                    cancelar();
                 }
                else
                    JOptionPane.showMessageDialog(new JFrame(),"La cedula no esta registrada","Retirar",JOptionPane.WARNING_MESSAGE);
            else
                JOptionPane.showMessageDialog(new JFrame(),"Cedula invalida","Retirar",JOptionPane.WARNING_MESSAGE);
        else
            JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Retirar",JOptionPane.WARNING_MESSAGE);

    }//GEN-LAST:event_btnRetirarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormularioBuscar().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRetirar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField txtApe;
    private javax.swing.JTextField txtCed;
    private javax.swing.JTextField txtNom;
    // End of variables declaration//GEN-END:variables

}
